'''
Created on 15.Oca.2010

@author: dturgut
'''

import sys
import avaHayyamIcons
from PyQt4 import QtGui, QtCore, Qt, QtSql

class Rubai(object):
    def __init__(self,ValueList=None):
        if ValueList is None:
            ValueList = [""]*7
        self.ID=ValueList[0]
        self.Parent=ValueList[1]
        self.Rubai=ValueList[2]
        self.Language=ValueList[3]
        self.Translator=ValueList[4]
        self.Reference=ValueList[5]
        #self.Tags=ValueList[6]
        
    def List(self):
        return [self.ID,self.Parent,self.Rubai,self.Language,self.Translator,self.Reference]
        #return [self.ID,self.Parent,self.Rubai,self.Language,self.Translator,self.Reference,self.Tags]



class AddEditRubai(QtGui.QDialog):
    def __init__(self,Parent=None,aRubai=None):
        QtGui.QDialog.__init__(self,Parent)
        self.myRubai=Rubai()
        if not aRubai:
            self.setWindowTitle("Add Rubai")
            self.setWindowIcon(QtGui.QIcon(":icons/Add.ico"))
        else:
            self.setWindowTitle("Edit Rubai")
            self.setWindowIcon(QtGui.QIcon(":icons/Edit.ico"))
            self.myRubai=aRubai
        self.CreateCentralWidget()
        self.FillRubaiWidgets()
    
    def CreateCentralWidget(self):
        MainLayout = QtGui.QGridLayout()
        OKCancelLayout = QtGui.QHBoxLayout()
        
        self.TreeRubai = QtGui.QTreeWidget()
        self.TreeRubai.setColumnCount(2)
        #0 Rubai First Line
        #1 ID
        self.TreeRubai.setSortingEnabled(True)
        self.TreeRubai.sortByColumn(0)
        self.TreeRubai.setHeaderHidden(True)
        self.TreeRubai.hideColumn(1)
        
        self.TextRubai = QtGui.QTextEdit()
        self.TextLanguage = QtGui.QLineEdit()
        self.TextTranslator = QtGui.QLineEdit()
        self.TextReference=QtGui.QLineEdit()
        self.ButtonOK = QtGui.QPushButton("OK")
        self.connect(self.ButtonOK,QtCore.SIGNAL("clicked()"),self.OKClicked)
        self.ButtonCancel = QtGui.QPushButton("Cancel")
        self.connect(self.ButtonCancel,QtCore.SIGNAL("clicked()"),self.reject)
        
        OKCancelLayout.addStretch()
        OKCancelLayout.addWidget(self.ButtonOK)
        OKCancelLayout.addWidget(self.ButtonCancel)
        
        MainLayout.addWidget(QtGui.QLabel("Parent"),0,4)
        MainLayout.addWidget(self.TreeRubai,1,4,3,1)
        MainLayout.addWidget(QtGui.QLabel("Rubai"),0,0,1,4)
        MainLayout.addWidget(self.TextRubai,1,0,1,4)
        MainLayout.addWidget(QtGui.QLabel("Language"),2,0)
        MainLayout.addWidget(self.TextLanguage,2,1)
        MainLayout.addWidget(QtGui.QLabel("Translator"),2,2)
        MainLayout.addWidget(self.TextTranslator,2,3)
        MainLayout.addWidget(QtGui.QLabel("Reference"),3,0)
        MainLayout.addWidget(self.TextReference,3,1,1,3)
        MainLayout.addLayout(OKCancelLayout,4,0,1,5)
        
        self.setLayout(MainLayout)
        
    def FillRubaiWidgets(self):
        self.TextLanguage.setText(self.myRubai.Language)
        self.TextTranslator.setText(self.myRubai.Translator)
        self.TextRubai.setText(self.myRubai.Rubai)
        self.TextReference.setText(self.myRubai.Reference)
        
        #Fill tree
        RootWidget = QtGui.QTreeWidgetItem(self.TreeRubai,["Root","0"])
        self.TreeRubai.setCurrentItem(RootWidget)
        for aRubai in self.parent().TreeRubaiList.findItems("0",Qt.Qt.MatchExactly,2):
            if str(aRubai.text(1))==self.myRubai.ID:
                continue
            QtGui.QTreeWidgetItem(RootWidget,[aRubai.text(0),aRubai.text(1)])
        self.TreeRubai.expandAll()
        if self.myRubai.ID!='':
            self.TreeRubai.setCurrentItem(self.TreeRubai.findItems(self.myRubai.Parent,Qt.Qt.MatchRecursive,1)[0])
        
    def OKClicked(self):
        self.myRubai.Parent=unicode(self.TreeRubai.selectedItems()[0].text(1))
        self.myRubai.Rubai=unicode(self.TextRubai.toPlainText())
        self.myRubai.Translator=unicode(self.TextTranslator.text())
        self.myRubai.Language=unicode(self.TextLanguage.text())
        self.myRubai.Reference=unicode(self.TextReference.text())
        self.accept()
        
class MainWindow(QtGui.QMainWindow):
    def __init__(self,Parent=None):
        QtGui.QMainWindow.__init__(self,Parent)
        self.setWindowTitle("avaHayyam")
        self.setWindowIcon(QtGui.QIcon(":icons/Rubai.ico"))
        self.DatabaseName = ""
        self.Database = QtSql.QSqlDatabase.addDatabase("QSQLITE")
        self.CreateCentralWidget()
        self.CreateActions()
        self.CreateToolbar()
        self.CreateMenu()
        
    def CreateCentralWidget(self):
        LayoutCentral = QtGui.QHBoxLayout()
        
        self.TreeRubaiList = QtGui.QTreeWidget()
        self.TreeRubaiList.setSortingEnabled(True)
        self.TreeRubaiList.setColumnCount(7)
        self.TreeRubaiList.sortByColumn(0,Qt.Qt.AscendingOrder)
        # Column 0: Rubai First Line
        # Column 1: ID
        # Column 2: Parent
        # Column 3: Rubai
        # Column 4: Language
        # Column 5: Translator
        # Column 6: Reference
        self.TreeRubaiList.hideColumn(1)
        self.TreeRubaiList.hideColumn(2)
        self.TreeRubaiList.hideColumn(3)
        self.TreeRubaiList.hideColumn(4)
        self.TreeRubaiList.hideColumn(5)
        self.TreeRubaiList.hideColumn(6)
        self.TreeRubaiList.setHeaderHidden(True)
        self.connect(self.TreeRubaiList,QtCore.SIGNAL("itemSelectionChanged()"),self.TriggeredTreeSelection)
        
        self.TextRubai = QtGui.QTextBrowser()

        Splitter = QtGui.QSplitter(Qt.Qt.Horizontal)
        Splitter.addWidget(self.TreeRubaiList)
        Splitter.addWidget(self.TextRubai)
        
        LayoutCentral.addWidget(Splitter)
        
        CentralWidget = QtGui.QWidget()
        CentralWidget.setLayout(LayoutCentral)
        self.setCentralWidget(CentralWidget)
        
    def CreateAction(self,text,slot=None,shortcut=None,icon=None,tip=None,checkable=False,signal="triggered()"):
        action=QtGui.QAction(text,self)
        if icon is not None:
            action.setIcon(QtGui.QIcon("%s.ico"%icon))
        if shortcut is not None:
            action.setShortcut(shortcut)
        if tip is not None:
            action.setToolTip(tip)
            action.setStatusTip(tip)
        if slot is not None:
            self.connect(action,QtCore.SIGNAL(signal),slot)
        if checkable:
            action.setCheckable(True)
        return action
    
    def CreateActions(self):
        self.ActionDatabaseNew = self.CreateAction("New Database...",self.TriggeredDatabaseNew,"Ctrl+N",":icons/New","Create a new database")
        self.ActionDatabaseOpen = self.CreateAction("Open Database...",self.TriggeredDatabaseOpen,"Ctrl+O",":icons/Open","Open existing database")
        self.ActionRubaiAdd = self.CreateAction("Add Rubai...",self.TriggeredRubaiAdd,"Ctrl+A",":icons/Add","Add a Rubai")
        self.ActionRubaiEdit = self.CreateAction("Edit Rubai...",self.TriggeredRubaiEdit,"Ctrl+E",":icons/Edit","Edit Rubai")
        self.ActionRubaiDelete = self.CreateAction("Delete Rubai",self.TriggeredRubaiDelete,"Ctrl+D",":icons/Delete","Delete Rubai")
        self.ActionTreeExpand = self.CreateAction("Expand All",self.TriggeredTreeExpand,"Ctrl+X",":icons/Expand","Expand all nodes")
        self.ActionTreeCollapse = self.CreateAction("Collapse All",self.TriggeredTreeCollapse,"Ctrl+C",":icons/Collapse","Collapse all nodes")
        self.ActionNavigateFirst = self.CreateAction("First Rubai",self.TriggeredNavigateFirst,"Ctrl+Up",":icons/First","Go to First Rubai")
        self.ActionNavigatePrevious = self.CreateAction("Previous Rubai",self.TriggeredNavigatePrevious,"Up",":icons/Previous","Go to Previous Rubai")
        self.ActionNavigateNext = self.CreateAction("Next Rubai",self.TriggeredNavigateNext,"Down",":icons/Next","Go to Next Rubai")
        self.ActionNavigateLast = self.CreateAction("Last Rubai",self.TriggeredNavigateLast,"Ctrl+Down",":icons/Last","Go to Last Rubai")
        self.ActionAbout = self.CreateAction("About...",self.TriggeredAbout,"F1",":icons/About","About avaHayyam")
        self.ActionExit = self.CreateAction("Exit",self.TriggeredExit,"Esc",":icons/Exit","Exit avaHayyam")
        
        self.UpdateActionStatus()
    
    def UpdateActionStatus(self):
        self.ActionRubaiAdd.setEnabled(self.Database.isOpen())
        self.ActionRubaiEdit.setEnabled(self.Database.isOpen() & bool(self.TreeRubaiList.selectedItems()))
        self.ActionRubaiDelete.setEnabled(self.Database.isOpen() & bool(self.TreeRubaiList.selectedItems()))
        self.ActionTreeExpand.setEnabled(self.Database.isOpen())
        self.ActionTreeCollapse.setEnabled(self.Database.isOpen())       
        self.ActionNavigateFirst.setEnabled(self.Database.isOpen() & bool(self.TreeRubaiList.selectedItems()))
        self.ActionNavigatePrevious.setEnabled(self.Database.isOpen() & bool(self.TreeRubaiList.selectedItems()))
        self.ActionNavigateNext.setEnabled(self.Database.isOpen() & bool(self.TreeRubaiList.selectedItems()))
        self.ActionNavigateLast.setEnabled(self.Database.isOpen() & bool(self.TreeRubaiList.selectedItems()))
    
    def CreateToolbar(self):
        Toolbar = QtGui.QToolBar()
        Toolbar.addAction(self.ActionDatabaseNew)
        Toolbar.addAction(self.ActionDatabaseOpen)
        Toolbar.addSeparator()
        Toolbar.addAction(self.ActionRubaiAdd)
        Toolbar.addAction(self.ActionRubaiEdit)
        Toolbar.addAction(self.ActionRubaiDelete)
        Toolbar.addSeparator()
        Toolbar.addAction(self.ActionTreeExpand)
        Toolbar.addAction(self.ActionTreeCollapse)
        Toolbar.addSeparator()
        Toolbar.addAction(self.ActionNavigateFirst)
        Toolbar.addAction(self.ActionNavigatePrevious)
        Toolbar.addAction(self.ActionNavigateNext)
        Toolbar.addAction(self.ActionNavigateLast)
        Toolbar.addSeparator()
        Toolbar.addAction(self.ActionAbout)
        Toolbar.addSeparator()
        Toolbar.addAction(self.ActionExit)
        
        self.addToolBar(Toolbar)
        
    def CreateMenu(self):
        MenuBar = QtGui.QMenuBar()
        MenuFile = MenuBar.addMenu("File")
        MenuRubai = MenuBar.addMenu("Rubai")
        MenuNavigate = MenuBar.addMenu("Navigate")
        
        MenuFile.addAction(self.ActionDatabaseNew)
        MenuFile.addAction(self.ActionDatabaseOpen)
        MenuFile.addSeparator()
        MenuFile.addAction(self.ActionAbout)
        MenuFile.addSeparator()
        MenuFile.addAction(self.ActionExit)
        
        MenuRubai.addAction(self.ActionRubaiAdd)
        MenuRubai.addAction(self.ActionRubaiEdit)
        MenuRubai.addAction(self.ActionRubaiDelete)
        
        MenuNavigate.addAction(self.ActionTreeExpand)
        MenuNavigate.addAction(self.ActionTreeCollapse)
        MenuNavigate.addSeparator()
        MenuNavigate.addAction(self.ActionNavigateFirst)
        MenuNavigate.addAction(self.ActionNavigatePrevious)
        MenuNavigate.addAction(self.ActionNavigateNext)
        MenuNavigate.addAction(self.ActionNavigateLast)
        
        self.setMenuBar(MenuBar)
        
    def TriggeredDatabaseNew(self):
        self.DatabaseNew()
        
    def TriggeredDatabaseOpen(self):
        self.DatabaseOpen()
    
    def TriggeredRubaiAdd(self):
        AddRubaiDialog = AddEditRubai(self)
        if AddRubaiDialog.exec_():
            ID = self.DatabaseRubaiAdd(AddRubaiDialog.myRubai)
            AddRubaiDialog.myRubai.ID=ID
            self.TreeRubaiAdd(AddRubaiDialog.myRubai)
        
    def TriggeredRubaiEdit(self):
        EditRubaiDialog = AddEditRubai(self,Rubai([self.TreeRubaiList.selectedItems()[0].text(i) for i in range(1,7)]))
        if EditRubaiDialog.exec_():
            SelectedItem=self.TreeRubaiList.selectedItems()[0]
            RubaisToEdit=[]
            for ChildIndex in range(SelectedItem.childCount()):
                ChildRubai=Rubai([self.TreeRubaiList.selectedItems()[0].child(ChildIndex).text(i) for i in range(1,7)])
                if EditRubaiDialog.myRubai.Parent!="0":
                    ChildRubai.Parent=EditRubaiDialog.myRubai.Parent
                RubaisToEdit.append(ChildRubai)
            RubaisToEdit.append(EditRubaiDialog.myRubai)
            for aRubai in RubaisToEdit:
                self.DatabaseRubaiEdit(aRubai)
                self.TreeRubaiEdit(aRubai)
    
    def TriggeredRubaiDelete(self):
        DeleteConfirmBox = QtGui.QMessageBox()
        DeleteConfirmBox.setIcon(QtGui.QMessageBox.Warning)
        DeleteConfirmBox.setWindowTitle("Delete Rubai")
        DeleteConfirmBox.setWindowIcon(QtGui.QIcon("Delete.ico"))
        DeleteConfirmBox.setText("Do you want to delete the selected Rubai?")
        DeleteConfirmBox.setStandardButtons(QtGui.QMessageBox.Ok | QtGui.QMessageBox.Cancel)
        if DeleteConfirmBox.exec_()==QtGui.QMessageBox.Ok:
            self.InfoClear()
            self.DatabaseRubaiDelete(Rubai([self.TreeRubaiList.selectedItems()[0].text(i) for i in range(1,7)]))
            # DeleteFromTree?
            self.TreeRubaiDelete(Rubai([self.TreeRubaiList.selectedItems()[0].text(i) for i in range(1,7)]))
                
    def TriggeredTreeExpand(self):
        self.TreeRubaiList.expandAll()
    
    def TriggeredTreeCollapse(self):
        self.TreeRubaiList.collapseAll()
    
    def TriggeredNavigateFirst(self):
        CurrentItem = self.TreeRubaiList.currentItem()
        while self.TreeRubaiList.itemAbove(CurrentItem):
            CurrentItem = self.TreeRubaiList.itemAbove(CurrentItem)
        self.TreeRubaiList.setCurrentItem(CurrentItem)
    
    def TriggeredNavigatePrevious(self):
        if self.TreeRubaiList.itemAbove(self.TreeRubaiList.currentItem()):
            self.TreeRubaiList.setCurrentItem(self.TreeRubaiList.itemAbove(self.TreeRubaiList.currentItem()))
    
    def TriggeredNavigateNext(self):
        if self.TreeRubaiList.itemBelow(self.TreeRubaiList.currentItem()):
            self.TreeRubaiList.setCurrentItem(self.TreeRubaiList.itemBelow(self.TreeRubaiList.currentItem()))
    
    def TriggeredNavigateLast(self):
        CurrentItem = self.TreeRubaiList.currentItem()
        while self.TreeRubaiList.itemBelow(CurrentItem):
            CurrentItem = self.TreeRubaiList.itemBelow(CurrentItem)
        self.TreeRubaiList.setCurrentItem(CurrentItem)
    
    def TriggeredTreeSelection(self):
        self.UpdateActionStatus()
        if self.TreeRubaiList.selectedItems():
            self.InfoUpdate()
    
    def TriggeredAbout(self):
        AboutBox = QtGui.QMessageBox()
        AboutBox.setIcon(QtGui.QMessageBox.NoIcon)
        AboutBox.setWindowTitle("About avaHayyam")
        AboutBox.setWindowIcon(QtGui.QIcon("Rubai.ico"))
        AboutBox.setText("avaHayyam\nby Avaris")
        AboutBox.setStandardButtons(QtGui.QMessageBox.Ok)
        AboutBox.exec_()
    
    def TriggeredExit(self):
        self.close()
    
    def DatabaseNew(self):
        self.DatabaseName = unicode(QtGui.QFileDialog.getSaveFileName(self,"New Database",".","avaHayyam Databases (*.adb)"))
        if self.DatabaseName:
            self.DatabaseCreate()
        else:
            pass # Cancel
    
    def DatabaseOpen(self):
        self.DatabaseName = unicode(QtGui.QFileDialog.getOpenFileName(self,"Open Database",".","avaHayyam Databases (*.adb)"))
        if self.DatabaseName:
            self.DatabaseConnect()
        else:
            pass # Cancel
    
    def DatabaseCreate(self):
        self.DatabaseClose()
        self.Database.setDatabaseName(self.DatabaseName)
        self.Database.open()
        if self.Database.isOpen():
            self.Database.exec_("""CREATE TABLE rubai (
                                id INTEGER PRIMARY KEY,
                                parent INTEGER,
                                rubai TEXT,
                                language TEXT,
                                translator TEXT,
                                reference TEXT)""")
            self.DatabaseClose()
            self.DatabaseConnect()
        else:
            print "Problem?"
    
    def DatabaseConnect(self):
        self.DatabaseClose()
        self.Database.setDatabaseName(self.DatabaseName)
        self.Database.open()
        if self.Database.isOpen():
            self.TreePopulate()
            self.UpdateActionStatus()
        else:
            print "Problem?"
            
    def DatabaseClose(self):
        if self.Database.isOpen():
            self.Database.close()
            
    def DatabaseRubaiAdd(self,aRubai):
        InsertQuery = QtSql.QSqlQuery()
        InsertQuery.prepare("INSERT INTO rubai (parent, rubai, language, translator, reference) "
                            "VALUES (:parent, :rubai, :language, :translator, :reference)")
        InsertQuery.bindValue(":parent",QtCore.QVariant(aRubai.Parent))
        InsertQuery.bindValue(":rubai",QtCore.QVariant(aRubai.Rubai))
        InsertQuery.bindValue(":language",QtCore.QVariant(aRubai.Language))
        InsertQuery.bindValue(":translator",QtCore.QVariant(aRubai.Translator))
        InsertQuery.bindValue(":reference",QtCore.QVariant(aRubai.Reference))
        InsertQuery.exec_()
        Query = self.Database.exec_("SELECT * FROM rubai")
        Query.last()
        return Query.value(0).toString()
    
    def DatabaseRubaiEdit(self,aRubai):
        InsertQuery = QtSql.QSqlQuery()
        InsertQuery.prepare("UPDATE rubai "
                            "SET parent=:parent, rubai=:rubai, language=:language, translator=:translator, reference=:reference "
                            "WHERE id=:id")
        InsertQuery.bindValue(":id",QtCore.QVariant(aRubai.ID))
        InsertQuery.bindValue(":parent",QtCore.QVariant(aRubai.Parent))
        InsertQuery.bindValue(":rubai",QtCore.QVariant(aRubai.Rubai))
        InsertQuery.bindValue(":language",QtCore.QVariant(aRubai.Language))
        InsertQuery.bindValue(":translator",QtCore.QVariant(aRubai.Translator))
        InsertQuery.bindValue(":reference",QtCore.QVariant(aRubai.Reference))
        InsertQuery.exec_()
    
    def DatabaseRubaiDelete(self,aRubai):
        InsertQuery = QtSql.QSqlQuery()
        InsertQuery.prepare("UPDATE rubai "
                            "SET parent='0'"
                            "WHERE parent=:id")
        InsertQuery.bindValue(":id",QtCore.QVariant(aRubai.ID))
        InsertQuery.exec_()
        InsertQuery.prepare("DELETE FROM rubai "
                            "WHERE id=:id")
        InsertQuery.bindValue(":id",QtCore.QVariant(aRubai.ID))
        InsertQuery.exec_()
    
    def TreePopulate(self):
        self.TreeRubaiList.clear()
        # Fill Root
        Query = self.Database.exec_("SELECT * FROM rubai WHERE parent='0'")
        while Query.next():
            self.TreeRubaiAdd(Rubai([Query.value(i).toString() for i in range(6)]))
        # Fill Childs
        # Fill Root
        Query = self.Database.exec_("SELECT * FROM rubai WHERE parent<>'0'")
        while Query.next():
            self.TreeRubaiAdd(Rubai([Query.value(i).toString() for i in range(6)]))
            
    def TreeRubaiAdd(self,aRubai):
        RubaiAsList = aRubai.List()
        RubaiAsList.insert(0,aRubai.Rubai.split("\n")[0])
        
        if aRubai.Parent=="0": # Root
            QtGui.QTreeWidgetItem(self.TreeRubaiList,RubaiAsList)
        else:
            QtGui.QTreeWidgetItem(self.TreeRubaiList.findItems(aRubai.Parent,Qt.Qt.MatchExactly,1)[0],RubaiAsList)
    
    def TreeRubaiEdit(self,aRubai):
        EditedItem = self.TreeRubaiList.findItems(aRubai.ID,Qt.Qt.MatchRecursive,1)[0]
        if EditedItem.text(2)=="0": #Root
            self.TreeRubaiList.takeTopLevelItem(self.TreeRubaiList.indexOfTopLevelItem(EditedItem))
        else:
            EditedItem.parent().takeChild(EditedItem.parent().indexOfChild(EditedItem))
        self.TreeRubaiAdd(aRubai)
        self.InfoUpdate()
    
    def TreeRubaiDelete(self,aRubai):
        EditedItem = self.TreeRubaiList.findItems(aRubai.ID,Qt.Qt.MatchRecursive,1)[0]
        if EditedItem.text(2)=="0": #Root
            for ChildIndex in range(EditedItem.childCount()):
                ChildItem=EditedItem.takeChild(ChildIndex)
                ChildItem.setText(2,"0")
                QtGui.QTreeWidgetItem(self.TreeRubaiList,[ChildItem.text(i) for i in range(6)])
            self.TreeRubaiList.takeTopLevelItem(self.TreeRubaiList.indexOfTopLevelItem(EditedItem))
        else:
            EditedItem.parent().takeChild(EditedItem.parent().indexOfChild(EditedItem))
    
    def InfoClear(self):
        self.TextRubai.clear()
    
    def InfoUpdate(self):
        self.InfoClear()
        for aRubaiLine in self.TreeRubaiList.selectedItems()[0].text(3).split("\n"):
            self.TextRubai.insertHtml(aRubaiLine+"<br>")
        self.TextRubai.insertHtml("<br>")
        self.TextRubai.insertHtml("<b>Language:<b> ")
        self.TextRubai.insertHtml(self.TreeRubaiList.selectedItems()[0].text(4)+"<br>")
        self.TextRubai.insertHtml("<b>Translator:<b> ")
        self.TextRubai.insertHtml(self.TreeRubaiList.selectedItems()[0].text(5)+"<br>")
        self.TextRubai.insertHtml("<b>Reference:<b> ")
        self.TextRubai.insertHtml(self.TreeRubaiList.selectedItems()[0].text(6)+"<br>")
        
#        if self.TreeRubaiList.selectedItems()[0].text(2)=="0": #Root
#            self.TextRubai.insertHtml("<b>Root index:<b> ")
#            self.TextRubai.insertHtml(str(self.TreeRubaiList.indexOfTopLevelItem(self.TreeRubaiList.selectedItems()[0])))
#        else:
#            self.TextRubai.insertHtml("<b>Child index:<b> ")
#            self.TextRubai.insertHtml(str(self.TreeRubaiList.selectedItems()[0].parent().indexOfChild(self.TreeRubaiList.selectedItems()[0])))
        
if __name__=="__main__":
    myApp = QtGui.QApplication(sys.argv)
    
    myHayyam = MainWindow()
    myHayyam.show()
    
    myApp.setStyle(QtGui.QStyleFactory.create("cleanlooks"))
    
    myApp.exec_()